#! /bin/sh
echo '########### begin to auto deploy ##########'

cd /usr/local/tomcat/bin

PID=$(jps -m | grep Bootstrap | grep -v grep|awk '{print $1}')

if [ -z "$PID" ];then
    echo "no tomcat processor"
else
    ./shutdown.sh
fi

sleep 2

./startup.sh

WAIT_SECONDS=30
k=1
TEST_URL="http://localhost:80/euc/system/alive"

for k in $(seq 1 $WAIT_SECONDS)
do
    sleep 2
    STATUS_CODE=`curl -o /dev/null -s -w %{http_code} $TEST_URL`
    if [ "$STATUS_CODE" = "200" ];then
        echo "request test_url succeeded!"
        echo "response code:  $STATUS_CODE"
        echo '########### deployed sucessfully ##########'
        exit 0
    else
        echo "request test_url failed!"
        echo "response code: $STATUS_CODE"
        echo "try one more time:the $k time....."
    fi
    if [ $k -eq $WAIT_SECONDS ];then
        echo "have tried 30 times, no more try"
        echo "########### deployed failure ##########"
        exit -1
    fi
done

